package com.thinkgem.jeesite.modules.sys.Scheduled;

import com.thinkgem.jeesite.modules.sys.entity.firm.Firm;
import com.thinkgem.jeesite.modules.sys.service.OfficeService;
import com.thinkgem.jeesite.modules.sys.service.SystemService;
import com.thinkgem.jeesite.modules.sys.service.customer.CustomerService;
import com.thinkgem.jeesite.modules.sys.service.defaulttime.DefaultTimeService;
import com.thinkgem.jeesite.modules.sys.service.firm.FirmService;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Lazy(false)
public class ScheduledTonTask {
    @Resource
    SystemService systemService;
    @Resource
    FirmService firmService;

    @Scheduled(cron = "0 */20 * * * ?")      //测试用，15秒执行
    public void huiDao() throws ParseException {
        List<Firm> list = firmService.getAll();
        if (list.size() > 0) {
            for (Firm firm : list) {
                if (firm.getHfStime() == null) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    Date before = sdf.parse(sdf.format(new Date()));
                    Date now = sdf.parse(sdf.format(firm.getHfFtime()));
                    if (sdf.format(firm.getHfFtime()).equals(sdf.format(new Date()))) {
                        firm.setYwStatic("2");
                    }
//                Calendar c1 = Calendar.getInstance();
//                Calendar c2 = Calendar.getInstance();
//                c1.setTime(new Date());
//                c2.setTime(firm.getHfFtime());
                    long l = now.getTime() - before.getTime();
                    long day = l / (24 * 60 * 60 * 1000);
                    firm.setHui(String.valueOf(day));
                    firmService.save(firm);
                }
                if (firm.getSlFtime() != null && firm.getYwStatic().equals("4")) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    Date before = sdf.parse(sdf.format(new Date()));
                    Date now = sdf.parse(sdf.format(firm.getSlFtime()));
                    if (sdf.format(firm.getSlFtime()).equals(sdf.format(new Date()))) {
                        firm.setYwStatic("5");
                    }
                    long l = now.getTime() - before.getTime();
                    long day = l / (24 * 60 * 60 * 1000);
                    firm.setShou(String.valueOf(day));
                    firmService.save(firm);
                }
                if (firm.getChuTime() != null) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    Date before = sdf.parse(sdf.format(new Date()));
                    Date now = sdf.parse(sdf.format(firm.getChuTime()));
                    long l = before.getTime() - now.getTime();
                    long day = l / (24 * 60 * 60 * 1000);
                    firm.setCxSx(String.valueOf(day));
                    firmService.save(firm);
                }
                if (firm.getSlTime() != null) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    Date before = sdf.parse(sdf.format(new Date()));
                    Date now = sdf.parse(sdf.format(firm.getSlTime()));
                    long l = before.getTime() - now.getTime();
                    long day = l / (24 * 60 * 60 * 1000);
                    firm.setSlSx(String.valueOf(day));
                    firmService.save(firm);
                }
            }
        }
    }
}
